package com.nameli.admin.config;

import cn.hutool.core.bean.BeanUtil;
import com.nameli.SIM.bean.entity.sys.SysUser;
import com.nameli.SIM.bean.vo.LoginVo;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.SIM.util.TokenUtil;
import com.nameli.admin.config.exception.ServiceException;
import com.nameli.admin.config.shiro.auth.TokenGenerator;
import org.apache.shiro.SecurityUtils;

/**
 * @author NAME-L
 * @Description 安全工具类
 * @className SecurityUtils
 * @date 2022-01-11 14:45:29
 */
public class SecurityTools {

    /**
     * 获取登录用户信息
     */
    public static SysUser getLoginUser() {
        try {
            return (SysUser)SecurityUtils.getSubject().getPrincipal();
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常" , ReturnStatus.FORBIDDEN);
        }
    }

    /**
     * 获取用户id
     */
    public static Long getUserId(){
        try {
            return getLoginUser().getUserId();
        } catch (Exception e) {
            throw new ServiceException("获取用户id异常" , ReturnStatus.FORBIDDEN);
        }
    }

    /**
     * 获取用户账号
     */
    public static String getUserName(){
        try {
            return getLoginUser().getUserName();
        } catch (Exception e) {
            throw new ServiceException("获取用户id异常" , ReturnStatus.FORBIDDEN);
        }
    }

    /**
     * 是否为管理员
     */
    public static boolean isAdmin(Long userId){
        return userId != null && 1L == userId;
    }

}
